**********************************************************************************
*Table: A3 - Heterogeneity based on being at the age limit for free vaccination	 *
*Code to run this file: 														 *
*texdoc do "${workdir}\Do files\Table A3 - het age.do"							 *
**********************************************************************************

reg vacunada_int i.tratamiento##i.edad17
local n1 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r21 = string(e(r2), "%9.3f")  
eststo model1_e

reg vacunada_int i.tratamiento##i.edad17 dosis_prev_int subsidiado i.ips ingreso
local n2 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r22 = string(e(r2), "%9.3f")  
eststo model2_e


local tratamiento_labels "Control Placebo Information `"Social norms"' Trust Framing"

// Generate the table with the estimated effects  
texdoc init "${tables}/TA3 het age (m).tex", replace
texdoc write \begin{table}[h!]
texdoc write \centering
texdoc write \caption{\centering Effect on HPV vaccine uptake by being at the age limit for free vaccination} 
texdoc write \small
texdoc write \label{hetage}
texdoc write \begin{tabular}{p{0.5\linewidth}>{\hsize=0.8\hsize}p{0.25\linewidth}>{\hsize=0.8\hsize}p{0.25\linewidth}}
texdoc write \hline
texdoc write  & \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
texdoc write \hline
texdoc write \addlinespace

texdoc write \textbf{Treatment} & & \\ 
// Write the estimates for i.tratamiento  
forval i=1(1)5 {   
  estimates restore model1_e   
  local b11 = string(_b[`i'.tratamiento], "%9.3f")  
  local se11 = string(_se[`i'.tratamiento], "%9.3f")  
  local pvalue11 = 2*ttail(e(df_r),abs(`b11'/`se11'))   
  stars `pvalue11'  
  local stars11 "`stars'"  
  
  estimates restore model2_e   
  local b21 = string(_b[`i'.tratamiento], "%9.3f")   
  local se21 = string(_se[`i'.tratamiento], "%9.3f")   
  local pvalue21 = 2*ttail(e(df_r),abs(`b21'/`se21'))   
  stars `pvalue21'  
  local stars21 "`stars'"  
  
  if `i' == 0 {   
    local trat_label "\ \ Control"   
  }   
  else if `i' == 1 {   
    local trat_label "\ \ Placebo"   
  }   
  else if `i' == 2 {   
    local trat_label "\ \ Information"   
  }   
  else if `i' == 3 {   
    local trat_label "\ \ Social norms"   
  }   
  else if `i' == 4 {   
    local trat_label "\ \ Trust"   
  }   
  else if `i' == 5 {   
    local trat_label "\ \ Framing"   
  }   
    
  // Write the results to the table   
  texdoc write  `trat_label' & `b11'`stars11' & `b21'`stars21' \\  
  texdoc write  & (`se11') & (`se21') \\  
}  
  
// Write the estimates for 1.edad17  
estimates restore model1_e   
local b12 = string(_b[1.edad17], "%9.3f")  
local se12 = string(_se[1.edad17], "%9.3f")  
local pvalue12 = 2*ttail(e(df_r),abs(`b12'/`se12')) 
stars `pvalue12'  
local stars12 "`stars'"  
  
estimates restore model2_e  
local b22 = string(_b[1.edad17], "%9.3f")   
local se22 = string(_se[1.edad17], "%9.3f")   
local pvalue22 = 2*ttail(e(df_r),abs(`b22'/`se22'))   
stars `pvalue22'  
local stars22 "`stars'"  
  
texdoc write & & \\
texdoc write  \textbf{Girl is 17 years old} & `b12'`stars12' & `b22'`stars22' \\  
texdoc write  & (`se12') & (`se22') \\  
  
texdoc write  \textbf{Treatment x At the age limit for free vaccination} & &  \\

// Write the estimates for i.tratamiento#1.edad17  
forval i=1(1)5 {   
  estimates restore model1_e  
  local b13 = string(_b[`i'.tratamiento#1.edad17], "%9.3f")  
  local se13 = string(_se[`i'.tratamiento#1.edad17], "%9.3f")  
  local pvalue13 = 2*ttail(e(df_r),abs(`b13'/`se13'))    
  stars `pvalue13'  
  local stars13 "`stars'"  
  
  local cons1    = string( _b[_cons], "%9.3f")  
  local se_cons1 = string(_se[_cons], "%9.3f")  
  local pv_cons1 = 2*ttail(e(df_r),abs(`cons1'/`se_cons1'))  
  stars `pv_cons1'  
  local stars_cons1 "`stars'"
   
  estimates restore model2_e   
  local b23 = string(_b[`i'.tratamiento#1.edad17], "%9.3f")   
  local se23 = string(_se[`i'.tratamiento#1.edad17], "%9.3f")   
  local pvalue23 = 2*ttail(e(df_r),abs(`b23'/`se23'))   
  stars `pvalue23'  
  local stars23 "`stars'"  
 
  local cons2    = string( _b[_cons], "%9.3f")  
  local se_cons2 = string(_se[_cons], "%9.3f")  
  local pv_cons2 = 2*ttail(e(df_r),abs(`cons2'/`se_cons2'))   
  stars `pv_cons2'  
  local stars_cons2 "`stars'"
  
  if `i' == 0 {   
    local trat_int_label "\ \ Control \& 17 years old"   
  }   
  else if `i' == 1 {   
    local trat_int_label "\ \ Placebo \& 17 years old"   
  }   
  else if `i' == 2 {   
    local trat_int_label "\ \ Information \& 17 years old"   
  }   
  else if `i' == 3 {   
    local trat_int_label "\ \ Social norms \& 17 years old"   
  }   
  else if `i' == 4 {   
    local trat_int_label "\ \ Trust \& 17 years old"   
  }   
  else if `i' == 5 {   
    local trat_int_label "\ \ Framing \& 17 years old"   
  }   
    
  // Write the results to the table   
  texdoc write  `trat_int_label' & `b13'`stars13' & `b23'`stars23' \\  
  texdoc write  & (`se13') & (`se23') \\  
}  

texdoc write & & \\
 texdoc write Constant  & \multicolumn{1}{c}{`cons1' `stars_cons1'} & \multicolumn{1}{c}{`cons2' `stars_cons2'}  \\
texdoc write 	& \multicolumn{1}{c}{(`se_cons1')}  & \multicolumn{1}{c}{(`se_cons2')}  \\

texdoc write \addlinespace
texdoc write \hline
texdoc write Strata fixed effects & No & Yes \\
texdoc write Covariates & No & Yes  \\
texdoc write R-squared & `r21' & `r22' \\
texdoc write Observations & `n1'  & `n2'  \\

texdoc write \hline	
texdoc write \addlinespace
texdoc write \multicolumn{3}{p{\linewidth}}{\footnotesize Models 1 and 2 estimate the interaction between the treatment variable and  whether the girl is under or exactly 17 years old (which is the age limit to receive free HPV vaccination). Model 2 includes as covariates whether a girl is under subsidized scheme, whether she has received a dose before, healthcare center visited and household income level. Standard errors in parentheses. *** p$<$0.001, ** p$<$0.01, * p$<$0.5} \\


// Close the table  
texdoc write \end{tabular}  
texdoc write \end{table}  
texdoc close